/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package codegener.entity;

import codegener.util.JsonUtils;
import codegener.util.StrUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author 32181
 */
public class Tabs {
    
    private final static List<TableInfo> checkTabs=new ArrayList<>();
    static{
        File check=new File("./conf/check_table");
        if(check.exists()){
            try{
                
                File[] tabs=check.listFiles();
                
                
                if(null!=tabs&&tabs.length>0){
                    for(File tab:tabs){
                        List<ColumnInfo> tempColumnInfos=readTab(tab); 
                        TableInfo tempTableInfo=new TableInfo();
                        tempTableInfo.setColumns(tempColumnInfos);
                        tempTableInfo.setName(tab.getName().split("\\.")[0]);
                        checkTabs.add(tempTableInfo);
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }else{
            System.out.println("dss.json文件不存在："+check.getAbsolutePath());
        }
        
        
    }
    
    public static List<TableInfo> getCheckTable(){
        return checkTabs;
    }
    
    public static TableInfo getCheckTable(String tableName){
        if(null!=checkTabs&&!checkTabs.isEmpty()){
            for(TableInfo tab:checkTabs){
                if(tab.getName().equals(tableName)){
                    return tab;
                }
            }
        }
        return null;
    }
    
    public static List<ColumnInfo> readTab(File tab){
        try{
            String content=FileUtils.readFileToString(tab, "UTF-8"); 
            String[] lines=content.split("\n");
            
             List<ColumnInfo> cis=new ArrayList<>();
            
            if(null!=lines&&lines.length>0){
                for(String line:lines){
                    ColumnInfo ci=new ColumnInfo();
                    String[] abs= line.split(",");
                    ci.setCname(abs[0].trim());
                    ci.setDataType(abs[1].trim());
                    cis.add(ci);
                }
            }
            return cis;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
